package com.slz3.business.module.systemManagement.sysMenu.repository;

import com.slz3.business.module.systemManagement.sysMenu.domain.SysMenu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.UUID;

public interface SysMenuRepository extends JpaRepository<SysMenu, UUID>, JpaSpecificationExecutor<SysMenu>, SysMenuCustomizedRepository {

    @Query("SELECT sm FROM SysMenu sm " +
            "WHERE sm.parentId IS NULL " +
            "AND sm.validFlag = 'VALID' " +
            "AND EXISTS ( " +
            " SELECT 1 FROM SysRoleMenu srm  " +
            " JOIN SysRoleUser sru ON sru.roleUuid = srm.roleUuid " +
            " WHERE srm.menuUuid = sm.uuid " +
            " AND sru.userUuid = :loginUuid " +
            ") order by sm.serNum")
    List<SysMenu> queryRootMenu(@Param("loginUuid") UUID loginUuid);

    @Query("SELECT sm FROM SysMenu sm " +
            "WHERE sm.parentId =:parentId " +
            "AND sm.validFlag = 'VALID' " +
            "AND EXISTS ( " +
            " SELECT 1 FROM SysRoleMenu srm  " +
            " JOIN SysRoleUser sru ON sru.roleUuid = srm.roleUuid " +
            " WHERE srm.menuUuid = sm.uuid " +
            " AND sru.userUuid = :loginUuid " +
            ") order by sm.serNum")
    List<SysMenu> queryChildrenMenu(@Param("loginUuid") UUID loginUuid, @Param("parentId") UUID parentId);

    @Query("SELECT sm FROM SysMenu sm where sm.parentId is null AND sm.validFlag = 'VALID' order by sm.serNum")
    List<SysMenu> queryByParentIdIsNull();

    @Query("SELECT sm FROM SysMenu sm where sm.parentId =:parentId AND sm.validFlag = 'VALID' order by sm.serNum")
    List<SysMenu> queryByParentId(@Param("parentId") UUID parentId);
}
